#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<errno.h>
//库函数strlen()
int my_strlen(const char* ch){
  int count = 0;
  assert(ch!=NULL);
  while(*ch++!='\0'){
    count++;
  }
  return count++;
}

//字符串拷贝 strcpy()
char* my_strcpy(char*dest,const char*src){
  char *ret = dest;
  assert(dest&&src);
  while(*dest++ = *src++){
    ;
  }
  return ret;//此处的返回值的设置,是为了链式访问
}
//字符串追加 strcat()
char * my_strcat(char * dest,char *src){
  char * ret = dest;
  assert(dest&&src);
  while (*dest)
  {
      dest++ ;
  }
  while(*dest++ = *src++){
    ;
  }
  return ret;
}
//字符串比较 strcmp()
int	 my_strcmp(const char *s1, const char *s2){
  assert(s1&&s2);
  while(*s1==*s2){
    if(*s1=='\0'){
      return 0;
    }
    s1++;
    s2++;
  }
  return *s1-*s2;
}

//判断是否有字串 strstr() 
char *my_strstr( char* str1,  char* str2 ){
   char* cur = str1;
   char* s1 = str1;
   char* s2 = str2;
   while (*cur)
   {
    s1 = cur;
    s2 = str2;
    while (*s1 && *s2 &&(*s1 == *s2 ))
    {
      s1++;
      s2++;
    }
    if(*s2=='\0'){
      return cur;
    }
    cur++;
   }
   return NULL;
}

//内存上的拷贝 memcpy() 我们所实现的my_memcpy无法实现数据重叠的问题,
//为了解决这个问题,我们实现需要实现另一个函数memmeov()
void* my_memcpy(void* dest,const void* src,size_t count){
  assert(dest&&src);
  void *ret = dest;
  while(count--){
    *(char *)dest = *(char *)src;
    dest = (char*)dest+1;
    src = (char*)src+1;
  }
  return ret;
}

//内存上的拷贝,可以解决数据的重叠
void* my_memmove(void* dest,const void* src,size_t count){
  assert(dest&&src);
  void * ret = dest;
  if(dest>src){
    while(count--){
      *((char*)dest+count)=*((char*)src+count);
    }
  }else{
    while(count--){
      *(char*)dest=*(char*)src;
      dest = (char*)dest+1;
      src = (char*)src+1;
    }
  }
  return ret;
}
int main(){
  // printf("%d",(int)(strlen("aaa")-strlen("dsadsada")));
  char arr1[] = "abcdef";
  char arr2[] = "bcde";
  int arr3 [] = {1,2,3,4,5,6,7,8,9};
  int arr4 [5] = {0};
  my_memmove(arr3,arr3+2,20);
  for (int i = 0; i < 9; i++)
  {
    printf("%d ",arr3[i]);
  }

  
}


